package com.lsongh.springBoot.controller;

import com.lsongh.springBoot.entity.City;
import com.lsongh.springBoot.entity.Result;
import com.lsongh.springBoot.service.CityService;
import com.lsongh.springBoot.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CityController {

    private static final Logger logger = LoggerFactory.getLogger(CityController.class);
    @Autowired
    CityService cityService;

    /**
     * 获取所有的城市信息
     *
     * @return
     */
    @RequestMapping(value = "/city", method = RequestMethod.GET)
    public Result<City> getAllCity() {
        return ResultUtil.success(cityService.findAll());
    }

    /**
     * 通过城市id来获取该城市的信息
     * Optional 解决空指针的问题
     */
    @RequestMapping(value = "/city/{id}", method = RequestMethod.GET)
    public Result getOneCity(@PathVariable("id") Integer id) {
        return ResultUtil.success(cityService.findById(id));
    }

    /**
     * 自定义sql查询
     * @param code
     * @return
     */
    @PostMapping("/city/code")
    public Result<City> getCityByCode(@RequestParam("code") String code){
        return ResultUtil.success(cityService.findCityByCode(code));

    }

}
